﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PM.Entity;
using PM.Services;

namespace PM.Site.Admin
{
    public partial class RegisterDividendAnnouncement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserTypeId"] == null)
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
            else if (Session["UserTypeId"] != null)
            {
                if (Int32.Parse(Session["UserTypeId"].ToString()) == 1)
                {
                    this.Loading();
                }
                else
                {
                    Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
                }

            }
            else
            {
                Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
            }

        }

        private void Loading()
        {

            if (!Page.IsPostBack)
            {
                DividendCollection dc = DividendServices.GetFutureDividendAnnouncementCollection();
                dlDividends.DataSource = dc;
                dlDividends.DataBind();

                dc = DividendServices.GetFutureScripDividendAnnounceCollection();
                dlScripDiv.DataSource = dc;
                dlScripDiv.DataBind();              

            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Dividend dividend = new Dividend();

            dividend.CompanyId = txbCompanyId.Text;
            dividend.Name = txbCompanyName.Text;
            DateTime announced = DateTime.MinValue;
            if (DateTime.TryParse(txbDOAnnouncement.Text, out announced))
            {
                dividend.AnnouncedDate = announced;
            }
            decimal rate = 0.00m;
            if (decimal.TryParse(txbDividendRate.Text,out rate))
            {
                dividend.RateOfDividend = rate;
            }

            decimal tax = 0.00m;
            if (decimal.TryParse(txbDividendTax.Text, out tax))
            {
                dividend.TaxRate = tax;
            }

            dividend.DividendType = txbDividendType.Text;
            dividend.FinancialYear = txbFinancialYear.Text;
            DateTime xd = DateTime.MinValue;
            if (DateTime.TryParse(txbXD.Text, out xd))
            {
                dividend.XD = xd;
            }
            DateTime paymentDate = DateTime.MinValue;
            if (DateTime.TryParse(txbPaymentDate.Text, out paymentDate))
            {
                dividend.Payment = paymentDate;
            }

            dividend.Remarks = txbRemarks.Text;
            DividendServices.Add(dividend);
            Response.Redirect("~/Admin/RegisterDividendAnnouncement.aspx");

        }

        protected void btnScripSave_Click(object sender, EventArgs e)
        {
            Dividend dividend = new Dividend();

            dividend.CompanyId = txbScripCompId.Text;
            dividend.Name = txbScripCompName.Text;
            DateTime announced = DateTime.MinValue;
            Int64 noOfnewShares = 0;
            Int64.TryParse(txbScripNoOfNewShares.Text, out noOfnewShares);
            dividend.NoOfNewSharesListed = noOfnewShares;
            if (DateTime.TryParse(txbScripAnnounDate.Text, out announced))
            {
                dividend.AnnouncedDate = announced;
            }
            decimal propotion = 0.00m;
            if (decimal.TryParse(txbScripDivPropotion.Text, out propotion))
            {
                dividend.Propotion = propotion;
            }            
            DateTime xd = DateTime.MinValue;
            if (DateTime.TryParse(txbScripXD.Text, out xd))
            {
                dividend.XD = xd;
            }
            DateTime paymentDate = DateTime.MinValue;
            if (DateTime.TryParse(txbScripDate.Text, out paymentDate))
            {
                dividend.Payment = paymentDate;
            }

            dividend.Remarks = txbScripRemarks.Text;
            DividendServices.Add(dividend);
            Response.Redirect("~/Admin/RegisterDividendAnnouncement.aspx");

        }
    }
}
